package a66weding.com.jiehuntong.fragment.category;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.andview.refreshview.XRefreshView;

import java.util.List;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.activity.CategorySearchActivity;
import a66weding.com.jiehuntong.activity.StoreDetailsActivity;
import a66weding.com.jiehuntong.adapter.CategoryAdapter_Case;
import a66weding.com.jiehuntong.application.DataWareHourse;
import a66weding.com.jiehuntong.application.GlobalApplication;
import a66weding.com.jiehuntong.beans.CaseBean;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.beans.User;
import a66weding.com.jiehuntong.common.api.Url;
import a66weding.com.jiehuntong.common.utils.CUtils;
import a66weding.com.jiehuntong.common.utils.SharesUtils;
import a66weding.com.jiehuntong.common.utils.Utils;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import a66weding.com.jiehuntong.inner.FragmentListener;
import a66weding.com.jiehuntong.inner.OnItemClickLitener;

/**
 * Created by admin on 2016/8/1.
 * 作用：搜索案例
 */
public class CaseFragment extends Fragment implements OnNetResultListener, OnItemClickLitener, FragmentListener {
    private View viewgroup;
    private String content = "";
    private RelativeLayout package_nodata;
    private CategorySearchActivity categorySearchActivity;
    private CaseBean bean;
    private boolean islogin;
    private int i_id;
    private int pageIndex = 1;
    private List<CaseBean.DataBean> dataBeen;
    private XRefreshView xRefreshView;
    private RecyclerView recyclerView;
    private boolean isDownRefresh = true;
    private CategoryAdapter_Case adapter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        categorySearchActivity = (CategorySearchActivity) getActivity();
        viewgroup = inflater.inflate(R.layout.fragment_package, null);
        package_nodata = (RelativeLayout) viewgroup.findViewById(R.id.package_nodata);
        initRefreshView(viewgroup);
        if (categorySearchActivity != null) {
            categorySearchActivity.setListener(this);
        }
        initData();
        return viewgroup;
    }

    @Override
    public void onSucess(SuccessBean sucessbean) {
        Object data = sucessbean.getData();
        if (isDownRefresh) {
            xRefreshView.stopRefresh();
        } else {
            xRefreshView.stopLoadMore();
        }
        if (data instanceof CaseBean) {
            bean = (CaseBean) data;
            switch (bean.getResCode()) {
                case "0000":
                    dataBeen = bean.getData();
                    if (dataBeen.size() > 0) {
                        pageIndex++;
                    }
                    if (isDownRefresh) {
                        adapter.clear();
                    }
                    adapter.append(dataBeen);
                    if (adapter.getSize() == 0) {
                        xRefreshView.setVisibility(View.INVISIBLE);
                        package_nodata.setVisibility(View.VISIBLE);
                    } else {
                        xRefreshView.setVisibility(View.VISIBLE);
                        package_nodata.setVisibility(View.INVISIBLE);
                    }
                    break;
                default:
                    CUtils.showToast(bean.getMsg());
                    break;
            }
        }
    }

    @Override
    public void onError(ErrorBean errorBean) {
    }

    /**
     * GET WeddingMobileApi/get_CasesListBySearch?Token={Token}&pageIndex={pageIndex}&category={category}&memberID={memberID}
     * 搜索专属 根据关键字搜索案例列表
     */
    private void getNetData(int pageIndex) {
        String url = Url.GET_CASESLISTBYSEARCH;
        url += "pageIndex=" + pageIndex + "&category=" + Utils.URLEncode(content) + "&memberID=" + i_id;
        HttpRequestBean bottomListRequestBean = new HttpRequestBean(RequestCode.GETREQUEST, url, this, CaseBean.class, RequestCode.GET_CASESLISTBYSEARCH);
        HttpHandleUtil.executeCommonGet(categorySearchActivity, bottomListRequestBean);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        setUserVisibleHint(hidden);
        super.onHiddenChanged(hidden);
    }

    public void initData() {
        islogin = ((GlobalApplication) categorySearchActivity.getApplication()).isLogin();
        if (islogin) {
            User user = DataWareHourse.getInstance().getmUser();
            if (user != null) {
                i_id = user.getData().get(0).getI_Id();
            }
        }
        content = SharesUtils.getInstance(categorySearchActivity).readXML("search");
        if (!TextUtils.isEmpty(content)) {
            pageIndex = 1;
            getNetData(pageIndex);
        }
    }

    //初始化上啦刷新
    private void initRefreshView(View viewgroup) {
        xRefreshView = (XRefreshView) viewgroup.findViewById(R.id.category_xrefreshview);
        //是否允许下拉刷新
        xRefreshView.setPullRefreshEnable(true);
        // 设置是否可以上拉加载
        xRefreshView.setPullLoadEnable(true);
        // 设置刷新完成以后，headerview固定的时间
        xRefreshView.setPinnedTime(1000);
        // 静默加载模式不能设置footerview
        // 设置支持自动刷新
        //    xRefreshView.setAutoLoadMore(true);
        recyclerView = (RecyclerView) viewgroup.findViewById(R.id.category_recycler_view_test_rv);
        recyclerView.setHasFixedSize(true);
        //线性布局
        recyclerView.setLayoutManager(new LinearLayoutManager(categorySearchActivity));
        adapter = new CategoryAdapter_Case(categorySearchActivity, null);
        adapter.setOnItemClickLitener(this);
        recyclerView.setAdapter(adapter);
        xRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh() {
                isDownRefresh = true;
                pageIndex = 1;
                getNetData(pageIndex);
            }

            @Override
            public void onLoadMore(boolean isSlience) {
                isDownRefresh = false;
                getNetData(pageIndex);
            }
        });
    }

    @Override
    public void onItemClick(View view, int position) {
        //查询案例详细 GET WeddingMobileApi/get_CasesDetail?Token={Token}&casesID={casesID}
        Intent intent = new Intent(categorySearchActivity, StoreDetailsActivity.class);
        CaseBean.DataBean dataBean = dataBeen.get(position);
        intent.putExtra(StoreDetailsActivity.STOREDETAILSACTIVITY, dataBean);
        categorySearchActivity.startActivity(intent);
    }

    @Override
    public void onDestroyView() {
        if (categorySearchActivity != null) {
            categorySearchActivity.removeListener(this);
        }
        super.onDestroyView();
    }

    @Override
    public void change(String content) {
        this.content = content;
        pageIndex = 1;
        getNetData(pageIndex);
    }
}
